package com.my.listeners;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class AttributeListener implements ServletRequestAttributeListener, HttpSessionAttributeListener, ServletContextAttributeListener {

    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        String attrName = servletContextAttributeEvent.getName();
        Object attrValue = servletContextAttributeEvent.getServletContext().getAttribute(attrName);
        System.out.println("ServletContext[增加]属性：" + attrName + "，属性值：" + attrValue);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        String attrName = servletContextAttributeEvent.getName();
        System.out.println("ServletContext[移除]属性：" + attrName);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        String attrName = servletContextAttributeEvent.getName();
        Object attrValue = servletContextAttributeEvent.getServletContext().getAttribute(attrName);
        System.out.println("ServletContext[修改]属性：" + attrName + "，属性值：" + attrValue);
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
        String attrName = servletRequestAttributeEvent.getName();
        Object attrValue = servletRequestAttributeEvent.getServletRequest().getAttribute(attrName);
        System.out.println("ServletRequest[增加]属性：" + attrName + "，属性值：" + attrValue);
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {
        String attrName = servletRequestAttributeEvent.getName();
        System.out.println("ServletRequest[移除]属性：" + attrName);
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {
        String attrName = servletRequestAttributeEvent.getName();
        Object attrValue = servletRequestAttributeEvent.getServletRequest().getAttribute(attrName);
        System.out.println("ServletRequest[修改]属性：" + attrName + "，属性值：" + attrValue);
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        String attrName = httpSessionBindingEvent.getName();
        Object attrValue = httpSessionBindingEvent.getSession().getAttribute(attrName);
        System.out.println("HttpSession[增加]属性：" + attrName + "，属性值：" + attrValue);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        String attrName = httpSessionBindingEvent.getName();
        System.out.println("HttpSession[移除]属性：" + attrName);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        String attrName = httpSessionBindingEvent.getName();
        Object attrValue = httpSessionBindingEvent.getSession().getAttribute(attrName);
        System.out.println("HttpSession[修改]属性：" + attrName + "，属性值：" + attrValue);
    }
}
